package com.cloud.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.entity.SysProjectAuth;
import com.cloud.enums.BusinessType;
import com.cloud.common.enums.ProjectStatus;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.system.service.ISysProjectAuthService;
import com.cloud.utils.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 项目授权管理
 *
 * @author yzj
 * @since 2022-12-07
 */
@Tag(name = "项目授权管理")
@RestController
@RequestMapping("/system/projectAuth")
public class SysProjectAuthController extends BaseController {

    @Autowired
    private ISysProjectAuthService sysProjectAuthService;

    @Operation(summary = "分页项目授权信息")
    @RequestPermissions("system:projectAuth:list")
    @GetMapping("/list")
    public Result list(SysProjectAuth projectAuth) {
        startPage();
        List<SysProjectAuth> list = sysProjectAuthService.selectSysProjectAuthList(projectAuth);
        return getDataTable(list);
    }

    @Operation(summary = "新增项目授权信息")
    @Logs(title = "新增项目授权", businessType = BusinessType.INSERT)
    @RequestPermissions("system:projectAuth:add")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysProjectAuth projectAuth) {
        if (StringUtils.isEmpty(projectAuth.getSysCode()) || StringUtils.isEmpty(projectAuth.getDomainName())
                || StringUtils.isEmpty(projectAuth.getAccessKey()) || StringUtils.isEmpty(projectAuth.getSecretKey())) {
            return Result.error("添加失败:请检查系统编号,域名地址,密钥是否为空");
        }
        return toResult(sysProjectAuthService.insertObject(projectAuth));
    }

    @Operation(summary = "修改项目授权信息")
    @Logs(title = "修改项目授权", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:projectAuth:edit")
    @PostMapping("/update")
    public Result update(@RequestBody SysProjectAuth projectAuth) {
        if (StringUtils.isEmpty(projectAuth.getSysCode()) || StringUtils.isEmpty(projectAuth.getDomainName())
                || StringUtils.isEmpty(projectAuth.getAccessKey()) || StringUtils.isEmpty(projectAuth.getSecretKey())) {
            return Result.error("添加失败:请检查系统编号,域名地址,密钥是否为空");
        }
        return toResult(sysProjectAuthService.updateObject(projectAuth));
    }

    @Operation(summary = "查询项目授权详情")
    @RequestPermissions("system:projectAuth:query")
    @GetMapping("/getInfo")
    public Result getInfo(Long id) {
        return Result.success("查询成功", sysProjectAuthService.selectProjectAuthInfo(id));
    }

    @Operation(summary = "删除项目授权信息")
    @Logs(title = "删除项目授权", businessType = BusinessType.DELETE)
    @RequestPermissions("system:projectAuth:remove")
    @PostMapping("/remove")
    public Result remove(String ids) {
        List<String> list = Arrays.asList(ids.split(StringPool.COMMA));
        return toResult(sysProjectAuthService.removeByIds(list));
    }

    // TODO Feign内部调用（根据类型查询密钥信息）
    @Operation(summary = "根据类型查询密钥信息", hidden = true)
    @RequestMapping(value = "/inAuth/selectProjectAuth", method = {RequestMethod.POST, RequestMethod.GET})
    public Result selectProjectAuth(SysProjectAuth projectAuth) {
        projectAuth.setIsApply(ProjectStatus.APPLIED.getCode());
        return Result.success("查询成功", sysProjectAuthService.selectApplyProjectAuth(projectAuth));
    }

}
